package com.cx.mall.goods.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.cx.mall.goods.entity.Brand;
import com.cx.mall.goods.entity.Category;
import com.cx.mall.goods.entity.CategoryBrand;
import com.cx.mall.goods.mapper.BrandMapper;
import com.cx.mall.goods.mapper.CategoryBrandMapper;
import com.cx.mall.goods.mapper.CategoryMapper;
import com.cx.mall.goods.service.IBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 品牌表 服务实现类
 * </p>
 *
 * @author yang
 * @since 2025-06-13
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {
    @Autowired
    private CategoryBrandMapper categoryBrandMapper;
    @Autowired // 注入分类DAO
    private CategoryMapper categoryMapper;
    @Override
    public List<Brand> listByOneCategoryId(int id) {
        LambdaQueryChainWrapper<Category> oneWrapper = new LambdaQueryChainWrapper<>(categoryMapper);
        List<Category> list = oneWrapper.eq(Category::getParentId, id).select(Category::getId)
                .list();
        List<Integer> ones = list.stream().map(Category::getId).collect(Collectors.toList());
        LambdaQueryChainWrapper<Category> twoWrapper = new LambdaQueryChainWrapper<>(categoryMapper);
        List<Category> list1 = twoWrapper.in(Category::getParentId, ones).select(Category::getId).list();
        List<Integer> twos = list1.stream().map(Category::getId).collect(Collectors.toList());
        LambdaQueryChainWrapper<CategoryBrand> threeWrapper = new LambdaQueryChainWrapper<>(categoryBrandMapper);
        List<CategoryBrand> list2 = threeWrapper.select(CategoryBrand::getBrandId).in(CategoryBrand::getCategoryId, twos).list();
        List<Integer> threes = list2.stream().map(CategoryBrand::getBrandId).collect(Collectors.toList());
        LambdaQueryChainWrapper<Brand> fourWrapper = new LambdaQueryChainWrapper<>(this.getBaseMapper());
        List<Brand> list3 = fourWrapper.select(Brand::getId, Brand::getLogo).in(Brand::getId, threes).list();
        return list3;
    }

    @Override
    public List<Brand> listByTwoCategoryId(int id) {
        return baseMapper.selectByTwoCategoryId(id);
    }

    @Override
    public List<Brand> listByThreeCategoryId(int id) {
        return baseMapper.selectByThreeCategoryId(id);
    }
}
